package Levels;

/**
 * Created by:
 * User: Igor Martynovich and Guzenko Igor
 * Date: 14.11.13
 */
public class LevelGenerator {
    public final int COUNT_PER_LEVEL=5;
    public static final int LEVEL_ONE = 7;
    public static final int LEVEL_TWO = 15;
    public static final int LEVEL_THREE = 23;

    public int[] getLevel(int levelsFromGenerator){
        int[] levelArray;
        switch (levelsFromGenerator){
            case LEVEL_ONE:
                levelArray=generateLevel(0,LEVEL_ONE,COUNT_PER_LEVEL);
                break;
            case LEVEL_TWO:
                levelArray=generateLevel(LEVEL_ONE,LEVEL_TWO,COUNT_PER_LEVEL);
                break;
            case LEVEL_THREE:
                levelArray=generateLevel(LEVEL_TWO,LEVEL_THREE,COUNT_PER_LEVEL);
                break;
            default:levelArray=generateLevel(0,LEVEL_ONE,COUNT_PER_LEVEL);
        }

        return levelArray;
    }

    private int[] generateLevel(int rangeStart, int rangeEnd, int questionsCount){
        int[] arr = new int[questionsCount];

        for(int pos = 0; pos < arr.length; pos++) {
            // assume rand exists elsewhere and we have access
            boolean invalid;
            do {
                int candidate = rangeStart + (int)(Math.random()*((rangeEnd-rangeStart)+1));

                invalid = false;
                for(int i = 0; i < pos; i++) {
                    if(arr[i] == candidate) {
                        invalid = true;
                        break;
                    }
                }

                arr[pos] = candidate;
            }
            while(invalid);
        }
        return arr;

    }
}
